package com.control.qct.businesssteward.adapter;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.control.qct.businesssteward.R;
import com.control.qct.businesssteward.model.GoodsChildTypeModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

/**
 * Created by Administrator on 2017/12/16 0016.
 */

public class GoodsTypeDetailAdapter extends BaseQuickAdapter<GoodsChildTypeModel, BaseViewHolder> {
    private IGetGoodsTypeName mIGetGoodsTypeName;

    public GoodsTypeDetailAdapter(@Nullable List<GoodsChildTypeModel> data, IGetGoodsTypeName iGetGoodsTypeName) {
        super(R.layout.rv_goods_type_detail_item, data);
        this.mIGetGoodsTypeName = iGetGoodsTypeName;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final GoodsChildTypeModel item) {
        holder.setText(R.id.tv_goods_type_name, item.getTitle());
        holder.addOnClickListener(R.id.tv_goods_type_name);
        TextView tv = holder.getView(R.id.tv_goods_type_name);
        holder.setGone(R.id.rv_goods_type_detail,item.getChildrens().size() != 0);
        holder.setTextColor(R.id.tv_goods_type_name, item.isChecked() ? Color.parseColor("#05C1AC") : Color
                .parseColor("#333333"));
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (item.getChildrens().size() == 0) {
                    if (item.isChecked()) {
                        item.setChecked(false);
                        holder.setTextColor(R.id.tv_goods_type_name, Color.parseColor("#333333"));
                    } else {
                        item.setChecked(true);
                        holder.setTextColor(R.id.tv_goods_type_name, Color.parseColor("#05C1AC"));
                    }
                    mIGetGoodsTypeName.getGoodsTypeName(item.getTitle(), item.getCategorySN(), "", item
                            .isChecked());
                }
            }
        });

        final RecyclerView rvGoodsTypeDetail = holder.getView(R.id.rv_goods_type_detail);
        rvGoodsTypeDetail.setLayoutManager(new GridLayoutManager(mContext, 3));
        GoodsTypeDetailChildAdapter goodsTypeDetailChildAdapter = new GoodsTypeDetailChildAdapter(item.getChildrens());
        rvGoodsTypeDetail.setAdapter(goodsTypeDetailChildAdapter);
        goodsTypeDetailChildAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CheckBox checkBox = (CheckBox) adapter.getViewByPosition(rvGoodsTypeDetail, position, R.id
                        .cb_goods_type_name);
                item.getChildrens().get(position).setChecked(checkBox.isChecked());
                //将选中的品类通过接口回传
                mIGetGoodsTypeName.getGoodsTypeName(item.getTitle(), item.getChildrens().get(position).getCategorySN
                        (), item.getChildrens().get(position).getTitle(), item.getChildrens().get(position).isChecked
                        ());
            }
        });
    }


    class GoodsTypeDetailChildAdapter extends BaseQuickAdapter<GoodsChildTypeModel.ChildrensBean, BaseViewHolder> {

        public GoodsTypeDetailChildAdapter(@Nullable List<GoodsChildTypeModel.ChildrensBean> data) {
            super(R.layout.rv_goods_type_detail_child_item, data);
        }

        @Override
        protected void convert(BaseViewHolder holder, GoodsChildTypeModel.ChildrensBean item) {
            holder.setText(R.id.cb_goods_type_name, item.getTitle());
            holder.setChecked(R.id.cb_goods_type_name, item.isChecked());
        }
    }

    /**
     * 实时获取点击状态，来更新选择的商品类别
     */
    public interface IGetGoodsTypeName {
        void getGoodsTypeName(String title, int categorySN, String name, boolean isChecked);
    }
}
